#!/bin/bash
# installation nginx

# 官网 nginx.org

. ./public_functions &>/dev/null #公共函数库
. /tmp/public_functions &>/dev/null #公共函数库

##1 源码安装nginx
nginx_Install_centos7() {

  if [[ -f /usr/local/nginx/sbin/nginx ]] || [[ $(find /run | grep nginx) ]] || [[ ! $(dpkg -L nginx | grep not) ]]; then
    logTips "Nginx may already exist" "(Nginx可能已安装,请手动检查)" #1-20#
    exit
  fi

  readonly nginx_Version="nginx-1.20.2.tar.gz"	# nginx压缩包名称
  readonly nginx_Catalog="nginx-1.20.2"		# 压缩包解压后的目录名称
  readonly nginx_Module="--user=nginx \
    --prefix=/usr/local/nginx \
    --with-http_ssl_module --with-select_module"  # nginx即将安装的模块

  logW "1. 开始安装nginx所需依赖" #1-20#
  logY "yum install gcc make pcre-devel openssl-devel tar zlib-devel net-tools -y" #1-20#
    for i in gcc make pcre-devel openssl-devel tar zlib-devel net-tools; do
      yum install $i -y
      checkTF #21#
    done

  logW "2. 开始下载 $nginx_Version" #1-20#
  logY "curl -# --output /tmp/$nginx_Version https://nginx.org/download/$nginx_Version" #1-20#
    if [[ ! -f /tmp/$nginx_Version ]]; then
      curl -# --output /tmp/$nginx_Version https://nginx.org/download/$nginx_Version
      checkTF #21#
    else
      logTips "" "$nginx_Version Already exists ($nginx_Version 安装包已存在)"
    fi

  logW "3. 开始解压 $nginx_Version" #1-20#
  logY "tar -xzf /tmp/$nginx_Version -C /tmp" #1-20#
    tar -xzf /tmp/$nginx_Version -C /tmp
  checkTF #21#

  logW "4. 进入nginx安装包目录 /tmp/$nginx_Catalog" #1-20#
  logY "cd /tmp/$nginx_Catalog" #1-20#
    cd /tmp/$nginx_Catalog
  checkTF #21#

  logW "5. 开始编译并安装 $nginx_Version" #1-20#
  logY "./configure $nginx_Module && make && make install" #1-20#
    ./configure $nginx_Module && make && make install
  checkTF #21#

  logW "6. 添加用户 nginx" #1-20#
  logY "useradd -M -s /sbin/nologin nginx" #1-20#
    useradd -M -s /sbin/nologin nginx
    
    if [[ $? -eq 0 || $? -eq 9 ]]; then
      logG "success ↑↑↑" #1-20#
      echo " "
    else
      logR "fail ↑↑↑       -->> return $?"
      exit
    fi

  logW "7. 创建用于nginx启动的systemd文件" #1-20#
  logY "cat >/usr/lib/systemd/system/nginx.service <<-EOF..." #1-20#
cat >/usr/lib/systemd/system/nginx.service <<-EOF
[Unit]
Description=The Nginx HTTP Server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPre=/usr/bin/rm -f /run/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -q
ExecStart=/usr/local/nginx/sbin/nginx -g "pid /run/nginx.pid;"
ExecReload=/usr/local/nginx/sbin/nginx -t -q
ExecReload=/usr/local/nginx/sbin/nginx -s reload -g "pid /run/nginx.pid;"

ExecStop=/bin/kill -s HUP \${MAINPID}
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=ture

[Install]
WantedBy=multi-user.target
EOF
  checkTF #21#
  logY "systemctl daemon-reload" #1-20#
    systemctl daemon-reload
  checkTF #21#

  logW "8. 启动nginx服务" #1-20#
  logY "systemctl enable --now  nginx" #1-20#
    systemctl enable --now  nginx
  checkTF #21#

  logW "9. 显示nginx信息和监听端口" #1-20#
  logY "/usr/local/nginx/sbin/nginx -V  && netstat -tupln | grep 'Proto|nginx'" #1-20#
    /usr/local/nginx/sbin/nginx -V
    echo "##-----------------------------------------------------------------------------------"
    netstat -tupln | grep -E "Proto|nginx"
  checkTF #21#

  logW "10. 放行80端口"
  get_Firewalld_status #23#
    if [[ $? -eq 0 ]]; then
      logY "firewall-cmd --zone=public --add-port="80"/"tcp" --permanent" #1-20#
      firewall-cmd --zone=public --add-port="80"/"tcp" --permanent 2>/dev/null
      firewall-cmd --reload &>/dev/null
      echo ""
    else
      logR "防火墙未开启" #1-20#
      echo ""
    fi

#  rm -rf /tmp/$nginx_Catalog ##删除nginx解压后的目录
}


##2 源码安装nginx
nginx_Install_ubuntu() {

  if [[ -f /usr/local/nginx/sbin/nginx ]] || [[ $(sudo find /run | grep nginx) ]] || [[ $(dpkg --list | grep nginx) ]]; then
    logTips "Nginx may already exist" "(Nginx可能已安装,请手动检查)" #1-20#
    exit
  fi

  readonly nginx_Version="nginx-1.20.2.tar.gz"  # nginx压缩包名称
  readonly nginx_Catalog="nginx-1.20.2"         # 压缩包解压后的目录名称
  readonly nginx_Module="--user=nginx \
    --prefix=/usr/local/nginx \
    --with-http_ssl_module --with-select_module"  # nginx即将安装的模块

  logW "1. 开始安装ngixn所需依赖" #1-20#
  logY "sudo apt-get install build-essential libpcre3 libpcre3-dev zlib1g-dev openssl libssl-dev make tar zlib-devel net-tools -y" #1-20#
      sudo apt-get update
    for i in build-essential libpcre3 libpcre3-dev zlib1g-dev openssl libssl-dev make tar net-tools; do
      sudo apt-get install $i -y
      checkTF #21#
    done

  logW "2. 开始下载 $nginx_Version" #1-20#
  logY "curl -# --output /tmp/$nginx_Version https://nginx.org/download/$nginx_Version" #1-20#
    if [[ ! -f /tmp/$nginx_Version ]]; then
      curl -# --output /tmp/$nginx_Version https://nginx.org/download/$nginx_Version
      checkTF #21#
    else
      logTips "" "$nginx_Version Already exists ($nginx_Version 安装包已存在)"
    fi

  logW "3. 开始解压 $nginx_Version" #1-20#
  logY "tar -xvf /tmp/$nginx_Version -C /tmp" #1-20#
    tar -xvf /tmp/$nginx_Version -C /tmp
  checkTF #21#

  logW "4. 进入nginx安装包目录 /tmp/$nginx_Catalog" #1-20#
  logY "cd /tmp/$nginx_Catalog" #1-20#
    cd /tmp/$nginx_Catalog
  checkTF #21#

  logW "5. 开始编译并安装 $nginx_Version" #1-20#
  logY "./configure $nginx_Module && sudo make && sudo make install" #1-20#
    ./configure $nginx_Module && sudo make && sudo make install
  checkTF #21#

  logW "6. 添加用户 nginx" #1-20#
  logY "sudo useradd -M -s /sbin/nologin nginx" #1-20#
    sudo useradd -M -s /sbin/nologin nginx

    if [[ $? -eq 0 || $? -eq 9 ]]; then
      logG "success ↑↑↑" #1-20#
      echo " "
    else
      logR "fail ↑↑↑       -->> return $?"
      exit
    fi

  logW "7. 启动nginx服务" #1-20#
  logY "sudo /usr/local/nginx/sbin/nginx" #1-20#
    sudo /usr/local/nginx/sbin/nginx
  checkTF #21#

  logW "8. 显示nginx信息和监听端口" #1-20#
  logY "/usr/local/nginx/sbin/nginx -V  && sudo netstat -tupln | grep 'Proto|nginx'" #1-20#
    /usr/local/nginx/sbin/nginx -V
    echo "##-----------------------------------------------------------------------------------"
    sudo netstat -tupln | grep -E "Proto|nginx"
  checkTF #21#

  logW "9. 放行80端口"
  get_Firewalld_status #23#
    if [[ $? -eq 0 ]]; then
      logY "firewall-cmd --zone=public --add-port="80"/"tcp" --permanent" #1-20#
      firewall-cmd --zone=public --add-port="80"/"tcp" --permanent 2>/dev/null
      firewall-cmd --reload &>/dev/null
      echo ""
    else
      logR "防火墙未开启" #1-20#
      echo ""
    fi

#  rm -rf /tmp/$nginx_Catalog ##删除nginx解压后的目录
}

check_Version #25#

systemtype=$(uname -a | grep -Eo '(ubuntu|centos7)')

if [[ $systemtype == centos7 ]]; then 
   nginx_Install_centos7  ##1

elif [[ $systemtype == ubuntu ]]; then
     nginx_Install_ubuntu  ##2

fi
